#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

struct date
{
	int year;
	int month;
	int day;
};
struct sta
{
	int name;
	int num;
	struct date input;
	struct date output;
};
int on_orbit(struct sta b,int year,int month,int day)
{
	if (year > b.input.year && year < b.output.year)
	{
		return 1;
	}
	else if (year == b.input.year&& year < b.output.year)
	{
		if (month > b.input.month)
		{
			return 1;
		}
		else if (month == b.input.month)
		{
			if (day >= b.input.day)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
	}
	else if (year == b.output.year)
	{
		if (month < b.output.month)
		{
			return 1;
		}
		else if (month == b.output.month)
		{
			if (day <= b.output.day)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	struct sta a[n];
	int sum=0;
	int i = 0;
	for (i = 0;i < n;i++)
	{
		scanf("%d", &a[i].name);
		scanf("%d", &a[i].num);
		scanf("%d-%d-%d", &a[i].input.year, &a[i].input.month, &a[i].input.day);
		scanf("%d-%d-%d", &a[i].output.year, &a[i].output.month, &a[i].output.day);
	}
	int year, month, day;
	scanf("%d-%d-%d", &year, &month, &day);
	for (i = 0;i < n;i++)
	{
		int b = on_orbit(a[i], year, month, day);
		if (b == 1)
		{
			sum += a[i].num;
		}
	}
	printf("%d", sum);
	return 0;
}